package com.fx.jfxsupport;

import javafx.application.Platform;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;

/**
 * A default standard splash pane implementation Subclass it and override it's
 * methods to customize with your own behavior. Be aware that you can not use
 * Spring features here yet.
 *
 * @author Felix Roske
 * @author Andreas Jay
 * @author Jason Sang
 * <p>
 * 默认登陆界面
 */

@Slf4j(topic = "程序启动界面")
public class SplashScreen implements Callable<Integer> {



	private static String DEFAULT_IMAGE = "/splash/start.png";

	Service<Object>  service = new Service<Object> () {

		@Override
		protected Task<Object>  createTask() {
			return new Task<Object>  () {
				Double i=1.00;

				@Override
				protected Object call() throws Exception {

					while (i++ < 100.00) {
						updateProgress(i, 100.00);
						updateValue(i+"%");
						try {
							Thread.sleep(50);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					return "检测通过";
				}
			};
		}

	};



	/**
	 * Override this to create your own splash pane parent node.
	 *
	 * @return A standard image
	 */
	public Parent getParent() {




		final ImageView imageView = new ImageView(getClass().getResource(getImagePath()).toExternalForm());

		//进度条最下方
		final ProgressBar splashProgressBar = new ProgressBar();
		splashProgressBar.setPrefWidth(imageView.getImage().getWidth());
		final VBox vbox = new VBox();
		VBox vbox2 = new VBox();
		Label completion = new Label() ;
		Platform.runLater(()->{
//            splashProgressBar.setProgress(0);
			splashProgressBar.progressProperty().bind(service.progressProperty());


			completion.textProperty().bind((service.valueProperty().asString()));

//            completion.textProperty().bind((service.progressProperty().asString()));
			service.start();
		});


		vbox.getChildren().addAll(imageView);
//		vbox2.setStyle("-fx-background:transparent;");
//		vbox2.getChildren().addAll(new Label("正在加载..."));
		vbox.getChildren().addAll(vbox2, splashProgressBar);
//		vbox.getChildren().addAll(imageView, splashProgressBar);
//		ProgressIndicator indicator = new ProgressIndicator(
//				ProgressIndicator.INDETERMINATE_PROGRESS);
//		indicator.autosize();
		StackPane zhantieban = new StackPane();
		zhantieban.getChildren().addAll(splashProgressBar);
//		zhantieban.getChildren().addAll();
		zhantieban.getChildren().addAll(completion);
		vbox.getChildren().addAll(zhantieban);
		return vbox;
	}


	/**
	 * Customize if the splash screen should be visible at all.
	 *
	 * @return true by default
	 */
	public boolean visible() {
		//设置延时


		return true;
	}

	/**
	 * Use your own splash image instead of the default one.
	 *
	 * @return "/splash/javafx.png"
	 */
	public String getImagePath() {
		return DEFAULT_IMAGE;
	}

	@Override
	public Integer call() throws Exception {
		return null;
	}
}
